﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GOFtest
{
    public partial class C2_Strategy : Form
    {
        private double total = 0;

        public C2_Strategy()
        {
            InitializeComponent();
            this.cobStrategy.SelectedIndex = 0;
        }

        private void btnCalculate_Click(object sender, EventArgs e)
        {
            CashContext cs = new CashContext(this.cobStrategy.SelectedItem.ToString());
            double totalPrice = 0;
            totalPrice =cs.ContextInterface(Convert.ToDouble(this.txtNum.Text) * Convert.ToDouble(this.txtPrice.Text));
            total += totalPrice;
            this.lblTotal.Text = total.ToString();
        }
  
        private void btnReset_Click(object sender, EventArgs e)
        {
            this.txtNum.Text = "";
            this.txtPrice.Text = "";
            this.cobStrategy.SelectedIndex = 0;
            this.lblTotal.Text = "";
            total = 0;
        }
                
    }

    //策略类，定义所有支持的算法的接口
    public abstract class CashStrategy
    {
        public abstract double CashInterface(double money);
    }

    //实现类，封装了具体的算法或行为，继承Strategy
    //正常消费
    public class CashNormal : CashStrategy
    {
        public override double CashInterface(double money)
        {
            return money;
        }
    }

    //打折类
    public class CashRebate : CashStrategy
    {
        private double moneyRebate = 1;

        public CashRebate(string moneyRebate)
        {
            this.moneyRebate = Convert.ToDouble(moneyRebate);
        }

        public override double CashInterface(double money)
        {
            return money*moneyRebate;
        }
    }

    //现金返利类
    public class CashReturn : CashStrategy
    {
        private double moneyCondition = 0;
        private double moneyReturn = 0;

        public CashReturn(double moneyCondition, double moneyReturn)
        {
            this.moneyCondition = moneyCondition;
            this.moneyReturn = moneyReturn;
        }

        public override double CashInterface(double money)
        {
            double result = 0;
            if (money >= moneyCondition)
            {
                result = money - Math.Floor(money / moneyCondition) * moneyReturn;
            }

            return result;
        }
    }

    //上下文类，用一个ConcreteStrategy来配置，维护一个对Strategy对象的引用
    public class CashContext
    {
        CashStrategy cs;

        //**参数并不是具体的收费策略对象，而是一个string表示收费类型。这里是简单工厂和策略模式的结合**
        public CashContext(string type)
        {
            switch (type)
            { 
                case "正常收费":
                    cs = new CashNormal();
                    break;
                case "打8折":
                    cs = new CashRebate("0.8");
                    break;
                case "满300送100":
                    cs=new CashReturn(300,100);
                    break;
            }
        }

        //上下文接口，根据具体的策略对象，调用其算法的方法(封装了具体的策略，客户端只与上下文有交互)
        public double ContextInterface(double money)
        {
            return cs.CashInterface(money);
        }
    }

}
